package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.domain.dto.result.ProductionPlanResult;
import com.example.domain.entity.ProductionPlan;
import com.example.domain.query.ProductionPlanQuery;
import com.example.factory.ResultFactory;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.service.IProductionPlanService;
import io.swagger.v3.oas.annotations.Operation;

/**
 * <p>
 * 生产计划表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2025-05-14
 */
@CrossOrigin
@RestController
@RequestMapping("/productionPlan")
@Tag(name = "生产计划管理器")
public class ProductionPlanController {

    @Autowired
    private IProductionPlanService productionPlanService;

    @Autowired
    private ResultFactory resultFactory;

    @PostMapping("/add")
    @Operation(summary = "新增生产计划")
    public boolean add(@RequestBody ProductionPlan productionPlan) {
        return productionPlanService.save(productionPlan);
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除生产计划")
    public boolean delete(@PathVariable Integer id) {
        return productionPlanService.removeById(id);
    }

    @PostMapping("/update")
    @Operation(summary = "更新生产计划")
    public boolean update(@RequestBody ProductionPlan productionPlan) {
        productionPlan.setAuditStatus(0);
        return productionPlanService.updateById(productionPlan);
    }

    @GetMapping("/get/{id}")
    @Operation(summary = "根据id查询生产计划")
    public ProductionPlanResult getById(@PathVariable Integer id) {
        return resultFactory.convert(productionPlanService.getById(id));
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询生产计划")
    public IPage<ProductionPlanResult> pageQuery(@RequestBody ProductionPlanQuery query,
                                                 @RequestParam(defaultValue = "1") Integer pageNum,
                                                 @RequestParam(defaultValue = "10") Integer pageSize) {
        return resultFactory.convertProductionPlanPage(productionPlanService.pageQuery(query, pageNum, pageSize));
    }

    @PostMapping("/audit/{id}/{status}")
    @Operation(summary = "审核生产计划")
    public boolean auditPlan(@PathVariable Integer id, @PathVariable Integer status) {
        return productionPlanService.auditPlan(id, status);
    }

    // 生产计划数
    @GetMapping("/planNum")
    @Operation(summary = "获取生产计划数")
    public Integer planeNum(Integer deptId) {
        return productionPlanService.getPlanNum(deptId);
    }

    // 生产计划完成数
    @GetMapping("/completedPlanNum")
    @Operation(summary = "获取生产计划完成数")
    public Integer completedPlanNum(Integer deptId) {
        return productionPlanService.getCompletedPlanNum(deptId);
    }
}
